package com.sdzee.dao;


import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.*;

import model.Event;
import model.Invitation;

@Stateless
public class InvitationDaoEJB {
	
	//Injection du Manager qui s'occupe de la connexion avec la BDD
		@PersistenceContext (unitName = "mydb_PU")
		private EntityManager em;
		
		@SuppressWarnings("unchecked")
		public List<Event> findInvitationAccepted(String pseudoUser){
			
			List<Event> list = new ArrayList<Event>() ;
			Query q = em.createQuery("SELECT e FROM Event e WHERE e.name in (select i.event from Invitation i where i.invited=:N)");
			q.setParameter("N", pseudoUser);
			try{
				list = (List<Event>) q.getResultList();
				return list;
			} catch(Exception e){throw new DAOException (e);}
		}
		
		
		public void add(Invitation invitation) throws DAOException {
			try{			
				em.persist(invitation);

			} catch(Exception e){throw new DAOException (e);}
			}

}
